/**
 * LEKU APPLIANCE CHAINS.
 * Copyright (c) 2016-2016 All Rights Reserved.
 */
package com.leku.mse.service.impl.oms;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.leku.commons.datasource.DataSourceContextHolder;
import com.leku.commons.util.CollectionUtil;
import com.leku.commons.util.StringUtil;
import com.leku.mse.constants.DataSourceConstant;
import com.leku.mse.dao.diary.DiaryDao;
import com.leku.mse.dao.oms.OrderDao;
import com.leku.mse.dto.oms.OrderDiaryDto;
import com.leku.mse.entity.diary.DiaryBaseEntity;
import com.leku.mse.entity.oms.OrderDiaryEntity;
import com.leku.mse.service.oms.OrderService;

/**
 * 功能描述:订单服务
 *
 * @author suiying
 * @created 2018年1月26日
 * @version 1.0.0
 */
@Service
public class OrderServiceImpl implements OrderService {

    private static final Logger LOG = LoggerFactory.getLogger(OrderServiceImpl.class);

    @Autowired
    private DiaryDao diaryDao;

    @Autowired
    private OrderDao orderDao;

    /*
     * (non-Javadoc)
     * @see com.leku.mse.service.oms.OrderService#dealOrderDiary(com.leku.mse.dto.oms.OrderDiaryDto)
     */
    @Override
    public void dealOrderDiary(OrderDiaryDto reqDto) {
        if (StringUtil.isEmpty(reqDto.getOrderId()) || StringUtil.isEmpty(reqDto.getUserId()))
            return;

        // 设置为手账数据库
        DataSourceContextHolder.setDataSourceType(DataSourceConstant.diaryDataSource());

        List<OrderDiaryEntity> orderDiaryList = orderDao.queryOrderDiary(reqDto.getUserId(), reqDto.getOrderId());

        if (CollectionUtil.isNotEmpty(orderDiaryList)) {
            DiaryBaseEntity diaryBase;

            for (OrderDiaryEntity orderDiary : orderDiaryList) {
                try {
                    diaryBase = diaryDao.getDiaryImg(orderDiary.getDiaryId());

                    orderDiary.setImg(diaryBase.getImg());
                    orderDiary.setImgHeight(diaryBase.getImgHeight());
                    orderDiary.setImgWidth(diaryBase.getImgWidth());
                    orderDiary.setDatas(diaryDao.getDiaryData(orderDiary.getDiaryId()));

                    orderDao.updateOrderDiary(orderDiary);
                } catch (Exception e) {
                    LOG.error("同步订单中手帐数据异常", e);
                }
            }
        }
    }

}
